<?php

class AuthController extends ControllerBase {

    public function indexAction() {
        $this->response->redirect('auth/login');
    }

    public function loginAction() {
        $identity = $this->auth->getIdentity();
        if ($identity) {
            return $this->response->redirect('/auth/profile');
        }
        $form = new LoginForm();
        if (!$this->request->isPost()) {

            if ($this->auth->hasRememberMe()) {
                $this->auth->loginWithRememberMe();
                return false;
            }
        } else {

            //valdate and print error in LoginForm function messages
            if ($form->isValid($this->request->getPost())) {
                try {
                    $this->auth->check(array(
                        'email' => $this->request->getPost('email'),
                        'password' => $this->request->getPost('password'),
                        'remember' => $this->request->getPost('remember'),
                    ));

                    if ($this->request->getQuery('url') == true) {
                        var_dump($this->request->getQuery('url'));
                        header('Location:' . $this->request->getQuery('url'));
                        return false;
                    }
                    return $this->response->redirect('');
                } catch (Exception $e) {
                    $form->appendMessage($e->getMessage(), "loginfail", null);
                }
            }
        }
        $this->view->form = $form;
    }

    public function registerAction() {
        $form = new RegisterForm();
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                $user = new Users();

                $user->assign(array(
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'name' => $this->request->getPost('name'),
                    'role' => 1,
                    'blocked' => 0,
                ));
                if ($user->save()) {
                    return $this->response->redirect('auth/login');
                } else {
                    foreach ($user->getMessages() as $value) {
                        $form->appendMessage($value, "registerFail", null);
                    }
                }
            }
        }
        $this->view->form = $form;
    }

    public function profileAction() {
        $identity = $this->auth->getIdentity();
        if (!$identity) {
            return $this->response->redirect('/auth/login?url=auth/profile');
        }

        $user = Users::findFirstById($identity['id']);
        if (!$user) {
            $this->auth->remove();
            return $this->response->redirect('/auth/login?url=auth/profile');
        }

        $form = new UserForm($user);
        if ($this->request->isPost()) {
            if ($form->isvalid($this->request->getPost())) {
                $user->email = $this->request->getPost('email', 'email');
                $user->name = $this->request->getPost('name', 'striptags');
                if ($user->save()) {
                    $form->appendMessage('Change information successfully', 'changeSuccess', null);
                    $this->session->set('auth-identity', array(
                        'id' => $user->id,
                        'email' => $user->email,
                        'role' => $user->role
                    ));
                } else {
                    foreach ($user->getMessages as $message) {
                        $form->appendMessage($message->getMessage(), $message->getField(), null);
                    }
                }
            }
        }
        $this->view->form = $form;
    }

    public function passwordAction() {
        $identity = $this->auth->getIdentity();
        if (!$identity) {
            return $this->response->redirect('/auth/login?url=auth/password');
        }

        $user = Users::findFirstById($identity['id']);
        if (!$user) {
            $this->auth->remove();
            return $this->response->redirect('/auth/login?url=auth/password');
        }

        $form = new PasswordForm();
        if ($this->request->isPost()) {
            if ($form->isvalid($this->request->getPost())) {
                $oldPassword = $this->request->getPost('passwordOld');
                try {
                    $this->auth->check(array(
                        'email' => $identity['email'],
                        'password' => $oldPassword,
                    ));
                    $user->password = $this->security->hash($this->request->getPost('passwordNew'));
                    if ($user->save()) {
                        $form->appendMessage('Change password successfully!', 'formMessage');
                    } else {
                        $form->appendMessage('Cannot change password by undefine message', 'formMessage');
                    }
                } catch (Exception $e) {
                    $form->appendMessage('Invalid old password', 'passwordOld');
                }
            }
        }
        $this->view->form = $form;
    }

}
